"""
    反向传播：
        1.乘法层
        2.加法层
"""

class MulLayer:
    def __init__(self):
        self.x = None
        self.y = None

    def forward(self,x,y):
        self.x = x
        self.y = y
        out = x * y

        return out

    def backward(self,dout):
        dx = dout * self.y
        dy = dout * self.x

        return dx,dy

class AddLayer:
    def __init__(self):
        pass

    def forward(self,x,y):
        out = x + y
        return out

    def backward(self,dout):
        dx = dout * 1
        dy = dout * 1
        return dx,dy



def test_buy_apple():
    apple = 100
    apple_num = 2
    tax = 1.1

    # layer
    mul_apple_layer = MulLayer()
    mul_tax_layer = MulLayer()

    # forward
    apple_price = mul_apple_layer.forward(apple, apple_num)
    price = mul_tax_layer.forward(apple_price, tax)
    print(f"the result of apple price is : {price}")

    # backward
    dprice = 1
    dapple_price,dtax = mul_tax_layer.backward(dprice)
    dapple,dapple_num = mul_apple_layer.backward(dapple_price)
    print(f"the result of apple price backward is : {dapple},{dapple_num}")

def test_buy_apple_orange():
    apple = 100
    orange = 150
    apple_num = 2
    orange_num = 3
    tax = 1.1

    # layer
    mul_apple_layer = MulLayer()
    mul_orange_layer = MulLayer()
    add_apple_orange_layer = AddLayer()
    mul_tax_layer = MulLayer()

    # forward
    apple_price = mul_apple_layer.forward(apple, apple_num)
    orange_price = mul_orange_layer.forward(orange, orange_num)
    all_price = add_apple_orange_layer.forward(apple_price, orange_price)
    result_price = mul_tax_layer.forward(all_price,tax)

    # backward
    dprice = 1
    dall_price = mul_tax_layer.backward(dprice)
    dapple_price,dapple_num = mul_apple_layer.backward(dall_price)
    dorange_price,dorange_num = add_apple_orange_layer.backward(dapple_price)
    print(f"The result price is : {result_price}")
    print(f"the apple data is : price->{dapple_price},num->{dapple_num}")
    print(f"the orange data is : price->{dorange_price},num->{dorange_num}")


if __name__ == '__main__':
    # test_buy_apple()
    test_buy_apple_orange()